package com.unicom.projt.fee;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.unicom.projt.Home;
import com.unicom.projt.InputPhoneNumberPage;
import com.unicom.projt.Main;
import com.unicom.projt.R;
import com.unicom.projt.util.*;

import java.text.DecimalFormat;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-5
 * Time: 下午9:46
 */
public class PaymentPage extends Activity implements View.OnClickListener {
    private double amount = 0.0;
    private Handler mHandler;

    private EditText mPaymentAmount;
    private boolean otherPhone = false;

    private ProgressDialog mDialog;
    private static final int DIALOG_PROGRESS = 18;
    private PaymentController mController;
    private IvrController mIvrController;

    String phoneNumber="";
    String cardPwd="";

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.payment);
        ((TextView)this.findViewById(R.id.payment_number_txt)).setText(UserHolder.phoneNumber);

        Bundle extras = getIntent().getExtras();
        phoneNumber = extras.getString("phoneNumber");

        if(UserHolder.phoneNumber.equals(phoneNumber)){
            otherPhone=false;
        }
        mController=new PaymentController(this);
        mIvrController=new IvrController(this);

        mPaymentAmount = (EditText) this.findViewById(R.id.payment_amount);

        this.findViewById(R.id.payment_next_btn).setOnClickListener(this);

        this.findViewById(R.id.payment_50_btn).setOnClickListener(this);
        this.findViewById(R.id.payment_100_btn).setOnClickListener(this);
        this.findViewById(R.id.payment_200_btn).setOnClickListener(this);
        this.findViewById(R.id.payment_other_btn).setOnClickListener(this);

        this.findViewById(R.id.payment_logout_btn).setOnClickListener(this);
        this.findViewById(R.id.payment_back_btn).setOnClickListener(this);


        initHandler();
    }

    private void initHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    default:
                        break;
                }
            }
        };
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.payment_50_btn:
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        changeAmountBtnColors(R.id.payment_50_btn);
                        setPaymentAccount(50);
                    }
                });
                break;
            case R.id.payment_100_btn:
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        changeAmountBtnColors(R.id.payment_100_btn);
                        setPaymentAccount(100);
                    }
                });
                break;
            case R.id.payment_200_btn:
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        changeAmountBtnColors(R.id.payment_200_btn);
                        setPaymentAccount(200);
                    }
                });
                break;
            case R.id.payment_other_btn:
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        changeAmountBtnColors(R.id.payment_other_btn);
                        mPaymentAmount.setText("0");
                        mPaymentAmount.setVisibility(View.VISIBLE);
                    }
                });
                break;
            case R.id.payment_next_btn:
                doPaymentConfirm();
                break;
            case R.id.payment_logout_btn:
                AlertDialogUtils.showYesNoDiaLog(this, "是否注销？", new AlertDialogUtils.Executor() {
                    @Override
                    public void execute() {
                        UserHolder.clean();
                        doLogout();
                    }
                });
                break;
            case R.id.payment_back_btn:
                PageJumpUtils.jump(this,PaymentInputPhoneNumberPage.class);
                this.finish();
                break;
        }
    }


    private void doLogout(){
        Intent intent = new Intent(this, InputPhoneNumberPage.class);
        this.startActivity(intent);
        this.finish();
    }
    private void changeAmountBtnColors(int id) {
        this.findViewById(R.id.payment_50_btn).setBackgroundDrawable(null);
        this.findViewById(R.id.payment_100_btn).setBackgroundDrawable(null);
        this.findViewById(R.id.payment_200_btn).setBackgroundDrawable(null);
        this.findViewById(R.id.payment_other_btn).setBackgroundDrawable(null);
        this.findViewById(id).setBackgroundDrawable(getResources().getDrawable(R.drawable.slider_03));
    }


    private void setPaymentAccount(int account) {
        mPaymentAmount.setText(account + "");
        mPaymentAmount.setVisibility(View.GONE);
    }

    private void doPaymentConfirm() {
        if (checkUserInput()) {
            try {
                amount = Double.valueOf(mPaymentAmount.getText().toString());
                amount= Double.valueOf(new DecimalFormat("###,###,###.##").format(amount));
            } catch (NumberFormatException nfe) {
                AlertDialogUtils.showConfirmDiaLog(this, "金额输入错误");
                return;
            }
            if(amount<0.01 || amount>1000){
                AlertDialogUtils.showConfirmDiaLog(this, "单笔充值金额在0.01元至1000元");
                return;
            }

            showDialog(DIALOG_PROGRESS);
            mController.sendPaymentInfo(phoneNumber,cardPwd,amount);
//            mIvrController.checkThePaymentPhoneNumber(UserHolder.phoneNumber,phoneNumber,amount);
        }
    }

    public void doUnionPay(){
        mController.sendPaymentInfo(phoneNumber,cardPwd,amount);
    }

    private boolean checkUserInput() {

        String amount = mPaymentAmount.getText().toString();
        if ("".equals(amount) || "0".equals(amount)) {
            AlertDialogUtils.showConfirmDiaLog(this, "请输入或选择金额");
            return false;
        }
//        else if ("".equals(cardNumber) || "".equals(cardPwd)) {
//            AlertDialogUtils.showConfirmDiaLog(this, "请输入银行卡号信息");
//            return false;
//        }
        return true;
    }

    @Override
    public void onBackPressed() {
        PageJumpUtils.jump(this, Home.class);
        this.finish();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_PROGRESS:
                mDialog = new ProgressDialog(this);
                mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                mDialog.setMessage("正在处理，请等待......");
                mDialog.setCancelable(false);
                mDialog.setOnKeyListener(new DialogInterface.OnKeyListener(){
                    public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {
                        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
                            return true;
                        }
                        return false;
                    }
                });
                return mDialog;
            default:
                return null;
        }
    }

    public void hideProgressDialog() {
        if (mDialog != null) {
            removeDialog(DIALOG_PROGRESS);
        }
    }
}
